const api = require('../../../../ws_micro_shop/utils/api.js');
const util = require('../../../../ws_micro_shop/utils/utils.js');
const app = getApp();

Page({
  data: {
    total_price: 0,
    address: null,
    express_price: 0.00,
    content: '',
    offline: 0,
    offlineClick: false,
    express_price_1: 0.00,
    name: '',
    mobile: '',
    integral_radio: 1,
    new_total_price: 0,
    show_card: false,
    payment: -1,
    show_payment: false,
    chooseAddr: false,
    chooseShop: false,
    attachment_url: app.siteInfo.attachment_url,
    open_setting: false,
    unit: '',
    integral_rule_check: 1,
    paytype: -1,
    total_price3: 0,
    btnFreezen: false,
    imgUrl: app.imgUrl,
    show_coupon_picker: false,
  },
  onLoad(options) {
    let that = this;
    let opts = options || {};
    let isMulti = app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.isMulti;  // 是否为多店
    let isSingle = app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.isSingle; // 是否为单店
    app.pageOnLoad(this);
    let time = util.formatData(new Date());
    app.loadShopSetting().then(res => {
      that.setData({
        checkmobile: res.check_mobile,
      });
    });
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
        currency: res.currency,
      });
    });
    this.setData({
      options: opts,
      store: wx.getStorageSync('store'),
      time,
      opts,
      isMulti,
      isSingle,
      version_type: app.ws_micro_shop_globalData.version_type,
    });
  },
  onShow() {
    let address = wx.getStorageSync('picker_address');
    if (address) {
      this.setData({
        address,
        name: address.name,
        mobile: address.mobile,
      });
      wx.removeStorageSync('picker_address');
    }
    this.getOrderData(this.data.options);
  },
  bindkeyinput(e) {
    this.setData({
      content: e.detail.value,
    });
  },
  KeyName(e) {
    this.setData({
      name: e.detail.value,
    });
  },
  KeyMobile(e) {
    this.setData({
      mobile: e.detail.value,
    });
  },
  getOffline(e) {
    let { pay_type_list, express_price: express, express_price_1: express_1, payment, isMulti, goods_info = [] } = this.data;
    let offline = Number(e.currentTarget.dataset.index);
   
    if (offline === 1) {
      this.setData({
        offline: 1,
        express_price: 0,
        express_price_1: express,
      });
    } else if (offline === 3) {
      this.setData({
        offline: 3,
        express_price: express_1,
      });
    } else {
      this.setData({
        offline: 0,
        express_price: express_1,
      });
    }

    // 切换物流方式时处理支付方式
    let new_pay_type_list = app.getPayTypeList(pay_type_list, offline, [goods_info], isMulti);
    ((offline !== 1 && payment === 6) || (offline === 1 && payment === 2)) && (payment = -1);  // 清空支付方式
    this.setData({
      offlineClick: true,
      new_pay_type_list,
      payment,
      indexNum: payment === -1 ? 0 : app.getpayment(new_pay_type_list, payment),
    });
    this.getPrice();
  },
  dingwei(e) {
    let that = this;
    let hit = e && e.currentTarget.dataset.flag;
    wx.getSetting({
      success(res) {
        if (typeof res.authSetting['scope.userLocation'] === 'undefined') {
          wx.getLocation({
            type: 'wgs84',
            altitude: false,
            success: res => {
              that.getShopLists(res.longitude, res.latitude);
            }
          });
        } else if (res.authSetting['scope.userLocation'] === false) {
          that.setData({
            open_setting: true,
          });
        } else {
          if (hit) { //点击了手动定位
            wx.chooseLocation({
              success: function (res) {
                that.setData({
                  location: res.address,
                });
                that.getShopLists(res.longitude, res.latitude);
              },
            });
          } else {
            wx.getLocation({
              type: 'wgs84',
              altitude: false,
              success: res => {
                that.getShopLists(res.longitude, res.latitude);
              }
            });
          }

        }
      },
    });
  },
  getShopLists(longitude, latitude) {
    let that = this;
    app.ws_micro_shoprequest({
      url: api.order.shop_list,
      data: {
        longitude,
        latitude,
        goods_id_str: that.data.goods_info && that.data.goods_info.goods_id + '',
        goods_type: 'miaosha'
      },
      success: function (res) {
        if (res.code === 0) {
          that.setData({
            shop_list: res.data,
          });
        }
      },
    });
  },
  orderSubmit(e) {
    let that = this;
    let offline = Number(that.data.offline);
    let { isMulti, isSingle, btnFreezen, require_verify, version_type } = that.data;

    // if (btnFreezen) {
    //   return false;
    // }

    that.setData({
      btnFreezen: true,
    })
    let data = {};
      if (that.data.payment === -1) {
        that.setData({
          btnFreezen: false,
        })
      return wx.showToast({
        title: '请选择支付方式',
        icon: 'none',
      });
    }
    if (offline === 0) {
      if (!that.data.address || !that.data.address.id) {
        that.setData({
          btnFreezen: false,
        })
        wx.showToast({
          title: '请选择收货地址',
          icon: 'none',
        });
        return;
      }
      data.address_id = that.data.address.id;
    } else if (offline === 3) {
      if (!that.data.address || !that.data.address.id) {
        that.setData({
          btnFreezen: false,
        })
        wx.showToast({
          title: '请选择收货地址',
          icon: 'none',
        });
        return;
      }
      data.address_id = that.data.address.id;
      // if (!that.data.shop || !that.data.shop.id) {
      //   wx.showToast({
      //     title: '请选择同城配送的门店',
      //     icon: 'none',
      //   });
      //   return;
      // }
      // data.shop_id = that.data.shop.id;
    } else {
      if (require_verify != 2) {
        data.address_name = that.data.name;
        data.address_mobile = that.data.mobile;
      }

      if (isSingle && !that.data.shop_list_length) {
        wx.showToast({
          title: '您购买的商品没有合适的自提站点',
          icon: 'none',
        });
        that.setData({
          btnFreezen: false,
        })
        return;
      }

      if (that.data.shop && that.data.shop.id) {
        data.shop_id = that.data.shop.id;
      } else {
        that.setData({
          btnFreezen: false,
        })
        wx.showToast({
          title: '请选择自提站点',
          icon: 'none',
        });
        return;
      }

      // =====add by cd 0625========
      if (offline === 1 && Number(require_verify) === 1) {
        if (!data.address_name) {
          that.setData({
            btnFreezen: false,
          })
          wx.showToast({
            title: '请填写收货人',
            icon: 'none',
          });
          return;
        }

        if (!data.address_mobile) {
          that.setData({
            btnFreezen: false,
          })
          wx.showToast({
            title: '请填写联系方式',
            icon: 'none',
          });
          return;
        }

        if (Number(that.data.checkmobile) === 1) {
          if (that.data.currency === 'CNY') {
            let check_mobile = /^1\d{10}$/;
            let check_phone = app.util.phoneExp.landline;
            if (!check_mobile.test(data.address_mobile) && !check_phone.test(data.address_mobile)) {
              that.setData({
                btnFreezen: false,
              })
              wx.showModal({
                title: '提示',
                content: '联系电话格式不正确',
                showCancel: false,
              });
              return;
            }
          } else {
            let check_canade = app.util.phoneExp.canadaPhone;
            if (!check_canade.test(data.address_mobile)) {
              that.setData({
                btnFreezen: false,
              })
              wx.showModal({
                title: '提示',
                content: '联系电话格式不正确',
                showCancel: false,
              });
              return;
            }
          }
        }
      }
    }

    app.getSubscribeMessage(0).then(response => {
    data.offline = offline;
    if (that.data.cart_id_list) {
      data.cart_id_list = JSON.stringify(that.data.cart_id_list);
    }
    if (that.data.goods_info) {
      data.goods_info = JSON.stringify(that.data.goods_info);
    }
    if (that.data.picker_coupon) {
      data.user_coupon_id = that.data.picker_coupon.user_coupon_id || 0;
    }
    if (that.data.content) {
      data.content = that.data.content;
    }
    wx.showLoading({
      title: '正在提交',
      mask: true,
    });
    if (Number(that.data.integral_radio) === 1) {
      data.use_integral = 1;
      data.use_forehead = that.data.integral.forehead;
    } else {
      data.use_integral = 2;
    }
    data.payment = that.data.payment;
    data.integral_rule_check = that.data.integral_rule_check;

    let last_share_guide_id = app.ws_micro_shop_globalData.last_share_guide_id;
    let last_share_good_id = Number(app.ws_micro_shop_globalData.last_share_good_id);
    if (version_type === '1' || version_type === '3') {
      let hasGoods = (last_share_good_id === Number(JSON.parse(data.goods_info).goods_id))
      if (last_share_guide_id && hasGoods) {
        data.staff_id = last_share_guide_id;
        data.staff_gid = last_share_good_id;
      } 
    }

    
    app.ws_micro_shoprequest({
      url: api.miaosha.submit,
      method: 'POST',
      data: data,
      success: function (res) {
        if (res.code === 0) {
          setTimeout(function () {
            that.setData({
              options: {},
            });
          }, 1);
          let order_id = res.data.order_id;
          if (Number(res.data.pay_price) <= 0) {
            app.util.navigateTo('/ws_micro_shop/pages/order/order', { status: -1 }, true);
            return;
          }
          if (Number(data.payment) === 0) {
            app.ws_micro_shoprequest({
              url: api.miaosha.pay_data,
              data: {
                order_id: order_id,
                pay_type: 'WECHAT_PAY',
              },
              success: function (res) {
                wx.hideLoading();
                if (res.code === 0) {
                  wx.requestPayment({
                    timeStamp: res.data.timeStamp,
                    nonceStr: res.data.nonceStr,
                    package: res.data.package,
                    signType: res.data.signType,
                    paySign: res.data.paySign,
                    success: function () {
                      wx.redirectTo({
                        url: '/ws_micro_shop/pages/order/order?status=1',
                      });
                    },
                    complete: function (e) {
                      if (e.errMsg === 'requestPayment:fail' || e.errMsg === 'requestPayment:fail cancel') {
                        wx.showModal({
                          title: '提示',
                          content: '订单尚未支付',
                          showCancel: false,
                          confirmText: '确认',
                          success: function (res) {
                            if (res.confirm) {
                              wx.redirectTo({
                                url: '/ws_micro_shop/pages/order/order?status=0',
                              });
                            }
                          },
                        });
                        return;
                      }
                      if (e.errMsg === 'requestPayment:ok') {
                        if (that.data.goods_card_list && that.data.goods_card_list.length > 0) {
                          that.setData({
                            show_card: true,
                            btnFreezen: false,
                          });
                        } else {
                          wx.redirectTo({
                            url: '/ws_micro_shop/pages/order/order?status=1',
                          });
                        }
                        return;
                      }
                      wx.redirectTo({
                        url: '/ws_micro_shop/pages/order/order?status=-1',
                      });
                    },
                  });
                  return;
                }
                if (res.code === 1) {
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                  });
                  setTimeout(function () {
                    app.util.navigateTo('/ws_micro_shop/pages/order/order', {
                      status: 0,
                    }, true);
                  }, 1000);
                  return;
                }
              },
            });
          }
          if (Number(data.payment) === 2) {
            app.ws_micro_shoprequest({
              url: api.miaosha.pay_data,
              data: {
                order_id: order_id,
                pay_type: 'HUODAO_PAY',
                form_id: e.detail.formId,
              },
              success: function (res) {
                wx.hideLoading();
                if (res.code === 0) {
                  wx.redirectTo({
                    url: '/ws_micro_shop/pages/order/order?status=1',
                  });
                } else {
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                  });
                  setTimeout(function () {
                    app.util.navigateTo('/ws_micro_shop/pages/order/order', {
                      status: 0,
                    }, true);
                  }, 1000);
                  return;
                }
              },
            });
          }
          if (Number(data.payment) === 3) {
            app.ws_micro_shoprequest({
              url: api.miaosha.pay_data,
              data: {
                order_id: order_id,
                pay_type: 'BALANCE_PAY',
                form_id: e.detail.formId,
              },
              success: function (res) {
                wx.hideLoading();
                if (res.code === 0) {
                  wx.redirectTo({
                    url: '/ws_micro_shop/pages/order/order?status=1',
                  });
                } else {
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                  });
                  setTimeout(function () {
                    app.util.navigateTo('/ws_micro_shop/pages/order/order', {
                      status: 0,
                    }, true);
                  }, 1000);
                  return;
                }
              },

            });
          }
          if (Number(data.payment) === 5) {
            app.ws_micro_shoprequest({
              url: api.miaosha.pay_data,
              data: {
                order_id: order_id,
                pay_type: 'CARD_PAY',
                form_id: e.detail.formId,
              },
              success: function (res) {
                wx.hideLoading();
                if (res.code === 0) {
                  wx.redirectTo({
                    url: '/ws_micro_shop/pages/order/order?status=1',
                  });
                } else {
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                  });
                  setTimeout(function () {
                    app.util.navigateTo('/ws_micro_shop/pages/order/order', {
                      status: 0,
                    }, true);
                  }, 1000);
                  return;
                }
              },

            });
          }
          if (data.payment === 6) {
            app.ws_micro_shoprequest({
              url: api.miaosha.pay_data,
              data: {
                order_id: order_id,
                pay_type: 'CLERK_PAY',
                form_id: e.detail.formId,
              },
              success: function (res) {
                if (res.code === 0) {
                  app.util.navigateTo('/ws_micro_shop/pages/order/order', { status: 1 }, true);
                } else {
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                  });
                  setTimeout(function () {
                    app.util.navigateTo('/ws_micro_shop/pages/order/order', {
                      status: 0,
                    }, true);
                  }, 1000);
                  return;
                }
              },
            });
          }
        }
        if (res.code === 1) {
          wx.hideLoading();
          that.setData({
            btnFreezen: false,
          })
          wx.showToast({
            title: res.msg,
            icon: 'none',
          });
          return;
        }
        if (res.code === 2) {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '由于您参与了优惠活动（满减、优惠券、积分抵扣、会员折扣等商城里的优惠活动）,购买商品后无法获得积分,确定购买吗',
            success(res) {
              if (res.confirm) {
                that.setData({
                  integral_rule_check: 2,
                  btnFreezen: false,
                });
                that.orderSubmit(e);
              } else if (res.cancel) {
                that.setData({
                  btnFreezen: false,
                })
                return;
              }
            }
          })
          return;
        }
      },
    });
    })
  },
  getOrderData(options) {
    let that = this;
    let address_id = '';
    let shop_id = '';
    let pay_type = -1;
    if (that.data.address && that.data.address.id) {
      address_id = that.data.address.id;
    }
    pay_type = that.data.paytype;
    if (that.data.shop && that.data.shop.id) {
      shop_id = that.data.shop.id;
    }
    if (options.goods_info) {
      wx.showLoading({
        title: '正在加载',
        mask: true,
      });
      app.ws_micro_shoprequest({
        url: api.miaosha.submit_preview,
        data: {
          goods_info: options.goods_info,
          address_id: address_id,
          shop_id: shop_id,
          pay_type: pay_type,
        },
        success: function (res) {
          wx.hideLoading();
          if (res.code === 0 || res.code === 3) {
            let shop_list = res.data.shop_list;
            let shop = false;
            if (res.data.is_shop) {
              shop = res.data.is_shop;
            }
            let dataObj = {
              total_price: res.data.total_price,
              total_price2: res.data.total_price2,
              couponPrice: res.data.total_price2,
              goods_list: res.data.list,
              goods_info: res.data.goods_info,
              userinfo: res.data.userinfo,
              address: res.data.address,
              coupon_list: res.data.coupon_list || [],
              couponid: res.data.coupon_list.length && res.data.coupon_list[0].user_coupon_id || 0,
              picker_coupon: res.data.coupon_list.length && res.data.coupon_list[0] || {},
              shop_list_length: shop_list.length,
              shop: shop,
              is_shop: res.data.is_shop,
              card: res.data.card,
              card_discount: res.data.card_discount,
              send_type: res.data.send_type,
              dispatch_type: res.data.dispatch_type,
              level: res.data.level,
              new_total_price: res.data.total_price2,
              default_integral: JSON.stringify(res.data.integral),
              integral: res.data.integral,
              is_payment: res.data.is_payment,
              is_coupon: res.data.list[0].coupon,
              is_discount: res.data.list[0].is_discount,
              pay_type_list: res.data.pay_type_list,
              gst_rate: res.data.gst_rate || 0,
              pst_rate: res.data.pst_rate || 0,
            };

            // that.setData({
            //   total_price3: res.data.total_price2,
            // });
            let picker_coupon = res.data.coupon_list && res.data.coupon_list.length > 0 && res.data.coupon_list[0];
            if (picker_coupon) {
              var a = 0;
              for (let index in res.data.coupon_list) {
                if (picker_coupon['user_coupon_id'] == res.data.coupon_list[index]['user_coupon_id'] && Number(res.data.list[0].coupon) === 1 ) {
                  res.data.total_price2 = res.data.total_price2 - picker_coupon['sub_price'];
                  a = 1;
                }
              }
              if (a == 0) {
                  //重置优惠券
                that.setData({
                  couponPrice: Number(res.data.total_price2),
                  picker_coupon: false,
                  show_coupon_picker: false,
                  integral: res.data.integral,
                });
              }        
            }
            
            if (!that.data.chooseAddr && !that.data.chooseShop) {
              that.setData(Object.assign(dataObj, {
                express_price: parseFloat(res.data.express_price),
                name: res.data.address ? res.data.address.name : '',
                mobile: res.data.address ? res.data.address.mobile : '',
              }));
            } else if (that.data.chooseAddr) {
              that.setData({
                express_price: parseFloat(res.data.express_price),
              });
            } else if (that.data.chooseShop) {
              that.setData(Object.assign(dataObj, {
                express_price: parseFloat(res.data.express_price),
              }));
            }
            if (!that.data.offlineClick) {
              if (Number(res.data.dispatch_type) !== 0) {
                if (Number(res.data.send_type) === 2) {
                  that.setData({
                    offline: 1,
                  });
                } else {
                  that.setData({
                    offline: 3,
                  });
                }
              } else {
                if (Number(res.data.send_type) === 2) {
                  that.setData({
                    offline: 1,
                  });
                } else {
                  that.setData({
                    offline: 0,
                  });
                }
              }
            }
            that.setData({
              couponPrice: res.data.total_price2,
            });

            // 处理支付方式展示数据
            let { pay_type_list, offline, goods_info, isMulti } = that.data;
            that.setData({
              new_pay_type_list: app.getPayTypeList(pay_type_list, offline, [goods_info], isMulti),
              require_verify: res.data.require_verify,
            })
            that.getPrice();
          }
          if (res.code === 1) {
            wx.showModal({
              title: '提示',
              content: res.msg,
              showCancel: false,
              confirmText: '确定',
              success: function (resp) {
                if (resp.confirm) {
                  wx.navigateBack({
                    delta: 1,
                  });
                }
              },
            });
          } else if (res.code === 3 && Number(that.data.offline) === 3) {
            wx.showModal({
              title: '提示',
              content: res.msg,
              showCancel: false,
              confirmText: '确定',
            });
          } else if (res.code === 2) {
            wx.showModal({
              title: '友情提示',
              content: '当前收货地址缺失经纬度，请重新编辑',
              showCancel: false,
              success: function (resp) {
                if (resp.confirm) {
                  app.util.navigateTo('ws_micro_shop/pages/address-edit/address-edit', { id: res.data.address_id, address_switch: res.data.address_switch }, false);
                }
              },
            });
          }
        },
      });
    }
  },
  copyText(e) {
    let text = e.currentTarget.dataset.text;
    if (!text) {
      return;
    }
    wx.setClipboardData({
      data: text,
      success: function () {
        wx.showToast({
          title: '已复制内容',
        });
      },
      fail: function () {
        wx.showToast({
          title: '复制失败',
          icon: 'none',
        });
      },
    });
  },
  showCouponPicker() {
    let { coupon_list, show_coupon_picker } = this.data;
    if (coupon_list && coupon_list.length > 0) {
      this.setData({
        show_coupon_picker: !show_coupon_picker,
      });
    }
  },

  disabledClick() {
    return false;
  },

  pickCoupon(e) {
    let that = this;
    let index = Number(e.currentTarget.dataset.index);
    let total_price2 = Number(this.data.total_price2);
    let couponPrice = total_price2;
    // if (that.data.total_price3 != total_price2) {
    //   total_price2 = couponPrice = that.data.total_price3;
    // }
    if (index === -1) {
      this.setData({
        couponPrice,
        picker_coupon: false,
        show_coupon_picker: false,
        couponid: -1,
        integral: JSON.parse(this.data.default_integral),
      });
    } else {
      let coupon = this.data.coupon_list[index];
      let integral = JSON.parse(this.data.default_integral);
      if (Number(integral.forehead) > Number(couponPrice)) {
        couponPrice = Number(integral.forehead);
      }
      couponPrice = Number(couponPrice - Number(coupon.sub_price)).toFixed(2);
      if (Number(couponPrice) < Number(integral.forehead)) {
        let rate = Number(integral.rate);
        integral.forehead = couponPrice;
        integral.forehead_integral = Math.ceil(couponPrice * rate);
      }
      this.setData({
        picker_coupon: coupon,
        couponid: coupon.user_coupon_id,
        couponPrice,
        integral,
        show_coupon_picker: false,
      });
    }
    this.getPrice();
  },
  numSub(num1, num2, length) {
    return 100;
  },
  showShop() {
    this.dingwei('');
    if (this.data.shop_list_length >= 1) {
      this.setData({
        show_shop: true,
        chooseShop: true,
      });
    }
  },
  showNearestShop() {
    this.dingwei('');
    if (this.data.shop_list_length >= 1) {
      this.setData({
        show_shop: true,
        chooseShop: true,
      });
    }
  },
  toPickAddr: function (e) {
    let param = e.currentTarget.dataset;
    this.setData({
      chooseAddr: true,
    });
    app.ws_micro_shop_navigate(param);
  },
  pickShop(e) {
    let index = Number(e.currentTarget.dataset.index);
    if (index === -1) {
      this.setData({
        shop: false,
        show_shop: false,
      });
    } else {
      this.setData({
        shop: this.data.shop_list[index],
        show_shop: false,
      });
    }
    this.getOrderData(this.data.options);
    this.getPrice();
  },
  integralSwitchChange(e) {
    if (e.detail.value) {
      this.setData({
        integral_radio: 1,
      });
    } else {
      this.setData({
        integral_radio: 2,
      });
    }
    this.getPrice();
  },
  integration(e) {
    let integration = this.data.integral.integration;
    wx.showModal({
      title: '积分使用规则',
      content: integration,
      showCancel: false,
      confirmText: '我知道了',
      confirmColor: '#ff4544',
    });
  },
  getPrice() {
    //默认选中优惠券优化  by zlh
    let { couponPrice: total_price2, express_price, picker_coupon, integral, integral_radio, offline, card_discount, gst_rate, pst_rate, payment } = this.data;
    let new_total_price = total_price2;
    integral_radio = Number(integral_radio);
    // let level = this.data.level;
    offline = Number(offline);              

    // if (level) {
    //   new_total_price = new_total_price * level.discount / 10;
    // }

    // if (picker_coupon) {
    //   new_total_price = new_total_price - picker_coupon.sub_price;
    // }

    if (integral && integral_radio === 1) {
      new_total_price = new_total_price - parseFloat(integral.forehead);
    }

    if (Number(payment) === 5 && card_discount) {
      new_total_price = new_total_price * card_discount / 10;
    }

    if (new_total_price < 0) {
      new_total_price = 0;
    }

    if (offline === 0 || offline === 3) {
      new_total_price = parseFloat(new_total_price) + express_price;
    }

    if (gst_rate > 0 || pst_rate > 0) {
      new_total_price = new_total_price + gst_rate + pst_rate;
    }
    this.setData({
      new_total_price: parseFloat(new_total_price).toFixed(2),
    });

  },
  formInput(e) {
    let index = e.currentTarget.dataset.index;
    let form = this.data.form;
    let form_list = form.list;
    form_list[index].default = e.detail.value;
    form.list = form_list;
    this.setData({
      form: form,
    });
  },
  selectForm(e) {
    let index = e.currentTarget.dataset.index;
    let k = Number(e.currentTarget.dataset.k);
    let form = this.data.form;
    let form_list = form.list;
    let default_list;
    if (form_list[index].type === 'radio') {
      default_list = form_list[index].default_list;
      for (let i in default_list) {
        if (Number(i) === k) {
          default_list[k].is_selected = 1;
        } else {
          default_list[i].is_selected = 0;
        }
      }
      form_list[index].default_list = default_list;
    }
    if (form_list[index].type === 'checkbox') {
      default_list = form_list[index].default_list;
      if (Number(default_list[k].is_selected) === 1) {
        default_list[k].is_selected = 0;
      } else {
        default_list[k].is_selected = 1;
      }
      form_list[index].default_list = default_list;
    }
    form.list = form_list;
    this.setData({
      form: form,
    });
  },
  showPayment() {
    this.setData({
      // show_payment: true,
    });
  },
  payPicker(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      payment: index,
    });
  },
  payClose(e) {
    let that = this;
    let { index } = e.detail;
    let { new_pay_type_list, payment: oldpayment = -1 } = that.data;
    let paytype = Number(new_pay_type_list[index].payment);
    if (Number(oldpayment) === paytype) return;
    that.setData({
      payment: paytype,
      paytype,
      indexNum: app.getpayment(new_pay_type_list, paytype),
    })
    if (Number(paytype) === 3) {
      wx.showModal({
        title: '友情提示',
        content: '是否确定选择账户余额支付',
        success: function (res) {
          if (res.confirm) {
            // that.getOrderData(that.data.options);
            that.setData({
              show_payment: false,
            });
          }
        },
      });
    } else if (Number(paytype) === 5) {
      let card = that.data.card;
      if (card) {
        // that.getOrderData(that.data.options);
        that.setData({
          show_payment: false,
        });
      } else {
        wx.showModal({
          title: '友情提示',
          content: '你还未开通会员卡，是否前往个人中心开通？',
          success: function (res) {
            if (res.confirm) {
              app.util.navigateTo('/ws_micro_shop/pages/user/user', {}, false);
            }
          },
        });
      }
    } else {
      // that.getOrderData(that.data.options);
      that.setData({
        show_payment: false,
      });
    }
    that.getPrice();
  },
});
